package com.greyu.ysj.service.impl;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.PageHelper;
import com.greyu.ysj.config.ResultStatus;
import com.greyu.ysj.entity.CbtChangeDraw;
import com.greyu.ysj.entity.CbtChangeDrawExample;
import com.greyu.ysj.entity.CbtInvest;
import com.greyu.ysj.entity.CbtInvestExample;
import com.greyu.ysj.entity.User;
import com.greyu.ysj.entity.UserExample;
import com.greyu.ysj.utils.InvestFlag;
import com.greyu.ysj.mapper.CbtInvestMapper;
import com.greyu.ysj.mapper.UserMapper;
import com.greyu.ysj.model.ResultModel;
import com.greyu.ysj.service.CbtInvestService;
import com.greyu.ysj.utils.NodeItem;
@Service
@Transactional
public class CbtInvestServiceImpl implements CbtInvestService {
	@Autowired
    private CbtInvestMapper cbtInvestMapper;
	@Autowired
	private UserMapper userMapper;
	
	@Override
	public ResultModel save(CbtInvest cbtInvest) {
				if (null == cbtInvest.getUserId() 
						||  null == cbtInvest.getInvestDate()
						||  null == cbtInvest.getCoinId()
						|| null == cbtInvest.getInvestAmount()
						|| null == cbtInvest.getInvestDuration()
		        		) {
		            return ResultModel.error(ResultStatus.DATA_NOT_NULL);
		        }
				//insert
		        this.cbtInvestMapper.insert(cbtInvest);
		        return ResultModel.ok(cbtInvest);
	}
	
	@Override
	public List<CbtInvest> getAllInvestList() {
		List<CbtInvest> cbtInvestList = this.cbtInvestMapper.selectAllInvestList();
		
        return cbtInvestList;
	}
	
	@Override
	public List<CbtInvest> getInvestListByuserId(Integer page, Integer rows, Integer userId) {
		if (null != page && null != rows) {
            PageHelper.startPage(page, rows);
        }
		
		List<CbtInvest> cbtInvestList = this.cbtInvestMapper.selectByuserId(userId);
        
        return cbtInvestList;
	}



	@Override
	public int totalPages(Integer userId, Integer rows) {
		int total=countInvestsByuserId(userId);
		int totalPages;
		if(total%rows==0) {
			totalPages=(int) (total/rows);
		}else {
			totalPages=(int) (total/rows)+1;
		}
		return totalPages;
	}

	@Override
	public CbtInvest getInvestById(int id) {
		CbtInvest cbtInvest=cbtInvestMapper.selectByPrimaryKey(id);
		return cbtInvest;
	}

	@Override
	public List<CbtInvest> getInvestRecordByuserId(Integer page, Integer rows, Integer userId) {
		if (null != page && null != rows) {
            PageHelper.startPage(page, rows);
        }
		List<CbtInvest> investRecord=cbtInvestMapper.selectInvestRecordByuserId(userId);
		if(investRecord == null || investRecord.size()<1) {
			
			return null;
		}
		return investRecord;
	}

	@Override
	public List<CbtInvest> getAllYesterdayInvestList() {
		List<CbtInvest> cbtInvestList = this.cbtInvestMapper.selectAllYesterdayInvestList();
	
        return cbtInvestList;
	}

	@Override
	public int investTotal(List<NodeItem> userIdList) {
		int index=0;
		for(NodeItem node : userIdList) {
			int userId=node.getUserId();
			int total=countTotalInvestByuserId(userId);
			if(total != 0) {
				index++;
			}
		}
		System.out.println("已投资的index=>"+index);
		return index;
	}

	@Override
	public int countInvestsByuserId(int userId) {
		int count=this.cbtInvestMapper.countInvestListByuserId(userId);
		return count;
	}

	@Override
	public int countTotalInvestByuserId(int userId) {
		int count=this.cbtInvestMapper.countTotalInvestByuserId(userId);
		return count;
	}

	@Override
	public List<CbtInvest> selectInvestsByuserId(Integer page, Integer rows, Integer userId) {
		if (null != page && null != rows) {
            PageHelper.startPage(page, rows);
        }
		List<CbtInvest> cbtInvestList = this.cbtInvestMapper.selectInvestsByuserId(userId);
		for(CbtInvest cbtInvest : cbtInvestList) {
			System.out.println("cbtInvest.getId()=>"+cbtInvest.getId());
		}
        return cbtInvestList;
	}

	@Override
	public int countRealInvestsByuserId(int userId) {
		int count =this.cbtInvestMapper.countRealInvestsByuserId(userId);
		return count;
	}

	@Override
	public int realTotalPages(Integer userId, Integer rows) {
		int total=countRealInvestsByuserId(userId);
		int totalPages;
		if(total%rows == 0) {
			totalPages=(int) (total/rows);
		}else {
			totalPages=(int) (total/rows)+1;
		}
		return totalPages;
	}

	@Override
	public ResultModel update(Integer id, CbtInvest invest) {
		CbtInvest cbtInvest=this.cbtInvestMapper.selectByPrimaryKey(id);
		if(cbtInvest == null) {
			return ResultModel.error(ResultStatus.DATA_NOT_FOUND);
		}
		if(invest.getInvestDate() != null) {
			cbtInvest.setInvestDate(invest.getInvestDate());
		}
		this.cbtInvestMapper.updateByPrimaryKey(cbtInvest);
		return ResultModel.ok(cbtInvest);
	}

	@Override
	public List<CbtInvest> getYesterdayInvestByuserId(Integer userId) {
		List<CbtInvest> list=this.cbtInvestMapper.selectYesterdayInvestByuserId(userId);
		return list;
	}
	
	@Override
	public double selectTotalInvestByuserId(Integer userId) {
		// TODO Auto-generated method stub
		return this.cbtInvestMapper.selectTotalInvestByuserId(userId);
	}
	
	@Override
	public double selectTotalUnExtractInvestByuserId(Integer userId) {
		// TODO Auto-generated method stub
		return this.cbtInvestMapper.selectTotalUnExtractInvestByuserId(userId);
	}
	
	@Override
	public List<CbtInvest> getAllValidInvestListByUserId(Integer id) {
		// TODO Auto-generated method stub
		CbtInvestExample cbtInvestExample = new CbtInvestExample();
		CbtInvestExample.Criteria criteria = cbtInvestExample.createCriteria();
		criteria.andExtractFlagEqualTo(0);
		criteria.andUserIdEqualTo(id);
		return this.cbtInvestMapper.selectByExample(cbtInvestExample);						
	}
	
	@Override
	public int updateBatch(List<InvestFlag> listArray) {
		// TODO Auto-generated method stub
		return this.cbtInvestMapper.updateBatch(listArray);
	}

	@Override
	public ResultModel updateFlag(Integer id, Integer flag) {
		CbtInvest cbtInvest=this.cbtInvestMapper.selectByPrimaryKey(id);
		if(cbtInvest == null) {
			return ResultModel.error(ResultStatus.DATA_NOT_FOUND);
		}
			cbtInvest.setExtractFlag(flag);
		
		this.cbtInvestMapper.updateByPrimaryKey(cbtInvest);
		return ResultModel.ok(cbtInvest);
	}

	@Override
	public double selectAllInvestByuserId(Integer userId) {
		
		UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andRefererEqualTo(userId);
        List<User> users = this.userMapper.selectByExample(userExample);
        List<Integer> list = users.stream().map(u->u.getUserId()).collect(Collectors.toList());
        double amount = 0.0;
        if (list.size()>0) {
        	 amount=this.cbtInvestMapper.selectTotalRewardByuserId(userId,list);
		}
		return amount;
	}

	@Override
	public double selectYesterdayRewardByuserId(Integer userId) {
		UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andRefererEqualTo(userId);
        List<User> users = this.userMapper.selectByExample(userExample);
        List<Integer> list = users.stream().map(u->u.getUserId()).collect(Collectors.toList());
        double amount = 0.0;
        if (list.size()>0) {
        	 amount=this.cbtInvestMapper.selectYesterdayRewardByuserId(userId,list);
		}
		return amount;
	}

	@Override
	public List<CbtInvest> getAllList() {
		
		return cbtInvestMapper.selectAllList();
	}

	@Override
	public double selectAllNoInvestByuserId(Integer userId) {
		// TODO Auto-generated method stub
		return cbtInvestMapper.selectAllNoInvestByuserId(userId);
	}
	
	@Override
	public double selectAllPriceByuserId(Integer userId) {
		// TODO Auto-generated method stub
		return cbtInvestMapper.selectAllPriceByuserId(userId);
	}
	
	@Override
	public List<CbtInvest> getChangeInv(Integer userId, Integer extractFlag) {
		// TODO Auto-generated method stub
		CbtInvestExample cbtInvestExample = new CbtInvestExample();
		CbtInvestExample.Criteria criteria = cbtInvestExample.createCriteria();
		criteria.andExtractFlagEqualTo(extractFlag);
		criteria.andUserIdEqualTo(userId);
		return this.cbtInvestMapper.selectByExample(cbtInvestExample);	
		
	}
	
	@Override
	public List<CbtInvest> selectAllInvestListByuserId(Integer userId) {
		
		UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andRefererEqualTo(userId);
        List<User> users = this.userMapper.selectByExample(userExample);
        List<Integer> list = users.stream().map(u->u.getUserId()).collect(Collectors.toList());
        double amount = 0.0;
        List<CbtInvest> lInvests = new ArrayList<>();
        if (list.size()>0) {
        	 lInvests = this.cbtInvestMapper.selectAllInvestListByuserId(userId,list);
        	 
		}
		return lInvests;
	}

	@Override
	public List<CbtInvest> investMoneyList(Integer page, Integer rows, Integer userId, Integer coinId, String startTime,
			String endTime) throws ParseException {
		// TODO Auto-generated method stub
		if (null != page && null != rows) {
            PageHelper.startPage(page, rows);
        }
		
		CbtInvestExample cbtInvestExample = new CbtInvestExample();
		CbtInvestExample.Criteria criteria = cbtInvestExample.createCriteria();
		SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
		if(userId!=null){
			criteria.andUserIdEqualTo(userId);
		}
		if(null!=startTime && null!=endTime && !startTime.equals("") && !endTime.equals("")){
			criteria.andInvestDateBetween(formatter.parse(startTime), formatter.parse(endTime));
		}
		if(null!=coinId){
			criteria.andCoinIdEqualTo(coinId);
		}
		
        List<CbtInvest> recharges = this.cbtInvestMapper.selectByExample(cbtInvestExample);
        if(recharges==null||recharges.size()<1) {
        	return null;
        }
        return recharges;
	}

	@Override
	public int ListTotal(Integer userId, Integer coinId, String startTime, String endTime, Integer rows)
			throws ParseException {
		// TODO Auto-generated method stub
		CbtInvestExample cbtInvestExample = new CbtInvestExample();
		CbtInvestExample.Criteria criteria = cbtInvestExample.createCriteria();
		SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
		if(userId!=null){
			criteria.andUserIdEqualTo(userId);
		}
		if(null!=startTime && null!=endTime && !startTime.equals("") && !endTime.equals("")){
			criteria.andInvestDateBetween(formatter.parse(startTime), formatter.parse(endTime));
		}
		if(null!=coinId){
			criteria.andCoinIdEqualTo(coinId);
		}
		int total=(int) this.cbtInvestMapper.countByExample(cbtInvestExample);
		int totalPages;
		if(total%rows == 0) {
			totalPages=(int) (total/rows);
		}else {
			totalPages=(int) (total/rows)+1;
		}
		System.out.println("totalPages_recharge=>"+totalPages);
		return totalPages;
		
	}

	@Override
	public double allInvest(Integer coinId,String startTime, String endTime) throws ParseException {
		// TODO Auto-generated method stub
		SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
		Date data1 =null;
		Date data2 =null;
		
		if(null!=startTime && null!=endTime && !startTime.equals("") && !endTime.equals("")){
			 data1 = formatter.parse(startTime);
			 data2 = formatter.parse(endTime);
		}
		return cbtInvestMapper.allInvest(coinId,data1, data2);
		 
	}
	
}
